/*
 * EasyCamera
 *
 * Copyright (c) 2020.  Frezrik frezrik@126.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.frezrik.common.file;

import android.graphics.Bitmap;
import android.graphics.Matrix;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;

public class BitmapUtils {

    public static byte[] BitmapToBGR(Bitmap image) {
        int bits = 4;
        int bytes = image.getByteCount();
        ByteBuffer buffer = ByteBuffer.allocate(bytes);
        image.copyPixelsToBuffer(buffer);
        byte[] temp = buffer.array();
        byte[] pixels = new byte[temp.length / bits * 3];
        int count = temp.length / bits;

        for (int i = 0; i < count; ++i) {
            pixels[i * 3 + 0] = temp[i * 4 + 0];
            pixels[i * 3 + 1] = temp[i * 4 + 1];
            pixels[i * 3 + 2] = temp[i * 4 + 2];
        }

        buffer.clear();
        buffer = null;
        temp = null;
        return pixels;
    }

    public static Bitmap BGRToBitmap(byte[] data, int width, int height) {
        try {
            int frameSize = width * height;
            if (frameSize <= 0) {
                return null;
            }

            int[] argb = new int[frameSize];

            for (int i = 0; i < height * width; ++i) {
                int b = Math.max(0, Math.min(255, data[i * 3 + 0] & 255));
                int g = Math.max(0, Math.min(255, data[i * 3 + 1] & 255));
                int r = Math.max(0, Math.min(255, data[i * 3 + 2] & 255));
                argb[i] = 0xFF000000 | (r << 16) | (g << 8) | b;
            }

            Bitmap bmp = Bitmap.createBitmap(argb, width, height, Bitmap.Config.ARGB_8888);
            argb = null;
            return bmp;
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
    }

    public static Bitmap RGBToBitmap(byte[] data, int width, int height) {
        try {
            int frameSize = width * height;
            int[] argb = new int[frameSize];

            for (int i = 0; i < height * width; ++i) {
                int r = Math.max(0, Math.min(255, data[i * 3 + 0] & 255));
                int g = Math.max(0, Math.min(255, data[i * 3 + 1] & 255));
                int b = Math.max(0, Math.min(255, data[i * 3 + 2] & 255));
                argb[i] = -16777216 + (r << 16) + (g << 8) + b;
            }

            Bitmap bmp = Bitmap.createBitmap(argb, width, height, Bitmap.Config.ARGB_8888);
            argb = null;
            return bmp;
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
    }

    public static void RecyleBitmap(Bitmap bitmap) {
        if (null != bitmap && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
        }

    }

    public static Bitmap rotate(Bitmap source, int angle, boolean turnHor, boolean turnVer) {
        return rotate(source, angle, false, turnHor, turnVer);
    }

    public static Bitmap rotate(Bitmap source, int angle, boolean isClockwise, boolean turnHor,
                                boolean turnVer) {
        if (null != source) {
            Matrix m = new Matrix();
            if (turnHor && !turnVer) {
                m.postScale(-1.0F, 1.0F);
            }

            if (!turnHor && turnVer) {
                m.postScale(1.0F, -1.0F);
            }

            if (turnHor && turnVer) {
                m.postScale(-1.0F, -1.0F);
            }

            if (angle > 0) {
                if (isClockwise) {
                    m.postRotate((float) (angle % 360));
                } else {
                    m.postRotate((float) (360 - angle % 360));
                }
            }

            try {
                Bitmap result = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                        source.getHeight(), m, true);
                return result;
            } catch (Throwable var7) {
                var7.printStackTrace();
            }
        }

        return source;
    }

    public static boolean save2Jpeg(Bitmap bitmap, String path, int quality) {
        return saveBitmap(bitmap, path, quality, Bitmap.CompressFormat.JPEG);
    }

    public static boolean saveToPNG(Bitmap bitmap, String path, int quality) {
        return saveBitmap(bitmap, path, quality, Bitmap.CompressFormat.PNG);
    }

    private static boolean saveBitmap(Bitmap bitmap, String path, int quality,
                                      Bitmap.CompressFormat format) {
        File file = new File(path);

        try {
            FileOutputStream out = new FileOutputStream(file);
            if (bitmap.compress(format, quality, out)) {
                out.flush();
                out.close();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

}
